import { DispImpl } from '@movinliao/h5-events'
import { kRoots } from './api'
import HashMap from '@movinliao/h5-hashmap'
import Root from './Root'

const YY = DispImpl.extend(function () {
  var self = this
  var _roots = new HashMap()

  var doInit = function () {
    for (var key in kRoots) {
      var block = kRoots[key]
      var root = new Root(block.Root, key)
      root.init(self, block.Methods, block.Signals)
      _roots.put(block.Key, root)
    }
  }

  var disconnect = function () {
    _roots.eachValue(function (root) {
      root && root.disconnect()
    })
  }

  this.constructor = function () {
    this.super('api.yyapi.')
  }

  this.init = function () {
    doInit()
  }

  this.destroy = function () {
    disconnect()
  }
  /**
     *停止
     */
  this.stop = function () {
    self.setPaused(true)
  }
  /**
     *开始
     */
  this.start = function () {
    self.setPaused(false)
  }

  this.roots = function () {
    return _roots || new HashMap()
  }

  this.log = function (content, tag) {
    console.info('====================', content, tag)
  }
})

var __yyapi = null

YY.impl = function () {
  return __yyapi || (__yyapi = new YY())
}

export default YY
